home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / p_util.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  2KB  |  70 lines

  1. /* p_util.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * PDFlib utility routines
  5.  */
  6.  
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. #include "p_intern.h"
  11.  
  12. /* 
  13.  * Note: although PDF doesn't impose any restrictions on
  14.  * the usable page size, Acrobat Reader and Exchange suffer
  15.  * from an architectural limit which allows pages to
  16.  * use sizes from 1 inch to 45 inches only (72-3240 pt, 2.54-114.3 cm).
  17.  */
  18.  
  19. PDF_pagesize 
  20.     a0        = {2380, 3368},        /* a0 is unusable in Acrobat 3! */
  21.     a1        = {1684, 2380},
  22.     a2        = {1190, 1684},
  23.     a3        = {842, 1190},
  24.     a4        = {595, 842},
  25.     a5        = {421, 595},
  26.     a6        = {297, 421},
  27.     b5        = {501, 709},
  28.     letter    = {612, 792},
  29.     legal     = {612, 1008},
  30.     ledger    = {1224, 792},
  31.     p11x17    = {792, 1224};
  32.  
  33. #define NO_OF_BUFS    6    /* number of static format buffers */
  34.  
  35. static char buf_array[NO_OF_BUFS][20];
  36. static int current_buf = 0;
  37.  
  38. /* Format floating point numbers in a PDF compatible way.
  39.  * This must be used for all floating output since PDF doesn't
  40.  * allow %g exponential format and %f produces too many characters
  41.  * in most cases.
  42.  * PDF spec says "use four or five decimal places".
  43.  *
  44.  * WARNING: This function uses a small number of cyclically reused
  45.  * static buffers. Don't use more than this number of pdf_float calls
  46.  * inside printf() or other function calls!!!
  47.  */
  48.  
  49. char *
  50. pdf_float(float f)
  51. {
  52.     char *buf = buf_array[(current_buf++) % NO_OF_BUFS];
  53.  
  54.     if (fabs(f) < 0.00001)
  55.     return "0";            /* force very small numbers to zero   */
  56.  
  57.     sprintf(buf, "%.4g", f);        /* try %g first and then check output */
  58.  
  59.     if (strchr(buf, 'e')) {        /* this format is not PDF compatible  */
  60.     if (fabs(f) < 1)
  61.         sprintf(buf, "%1.5f", f);    /* 5 decimal places for small numbers */
  62.     else if (fabs(f) < 1000)
  63.         sprintf(buf, "%1.2f", f);    /* 2 decimal places for medium numbers*/
  64.     else
  65.         sprintf(buf, "%1.0f", f);    /* skip decimal places for big numbers*/
  66.     }
  67.  
  68.     return buf;
  69. }
  70.